package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.wx.dao.GoodsProductDao;
import com.bootdo.wx.dao.MemberStockDao;
import com.bootdo.wx.domain.MemberStockDO;
import com.bootdo.wx.service.MemberStockService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;



@Service
public class MemberStockServiceImpl implements MemberStockService {
	@Autowired
	private MemberStockDao memberStockDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	
	@Override
	public MemberStockDO get(String id){
		return memberStockDao.get(id);
	}
	
	@Override
	public List<MemberStockDO> list(Map<String, Object> map){
		return memberStockDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberStockDao.count(map);
	}
	
	@Override
	public int save(MemberStockDO memberStock){
		return memberStockDao.save(memberStock);
	}
	
	@Override
	public int update(MemberStockDO memberStock){
		return memberStockDao.update(memberStock);
	}
	
	@Override
	public int remove(String id){
		return memberStockDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberStockDao.batchRemove(ids);
	}

    @Override
    public List<MemberStockDO> likeList(Map<String, Object> map){
        return memberStockDao.likeList(map);
    }

	@Override
	public int findStockNumByUserId(Map<String, Object> map) {
		return  memberStockDao.findStockNumByUserId(map);
	}

	@Override
	public R getMemberStockList(Map<String, Object> params) {
		Map<String, Object> productMap = new HashMap<>();
		productMap.put("delFlag", "0");
		productMap.put("disabled", "0");
		
		//添加库存
		List<Map<String, Object>> list = goodsProductDao.getByNotInStock(params.get("userId").toString());
		if(list.size()>0){
			for (Map<String, Object> map : list) {
				productMap.put("goodsId", map.get("goodsId").toString());
				productMap.put("productId", map.get("productId").toString());
				productMap.put("userId", params.get("userId").toString());
				productMap.put("delFlag", "0");
				List<MemberStockDO> list2 = memberStockDao.list(productMap);
				if(list2.size()<=0){
					MemberStockDO memberStockDO = new MemberStockDO();
					memberStockDO.setCaAt(new Date());
					memberStockDO.setCause("后台修改");
					memberStockDO.setDelFlag(0);
					memberStockDO.setGoodsId(map.get("goodsId").toString());
					memberStockDO.setId(UUIDUtils.getUUID());
					memberStockDO.setProductId(map.get("productId").toString());
					memberStockDO.setStock(0);
					memberStockDO.setUserId( params.get("userId").toString());
					memberStockDao.save(memberStockDO);
				}
				
			}
		}
		if(params.get("flag").equals("0")){
			PageHelperUtils.page(params);
			List<Map<String, Object>> mapList = memberStockDao.getMemberStockList(params);
			PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(mapList);
			return R.ok().put("data", pageInfo);
		}else{
			List<Map<String, Object>> mapList = memberStockDao.getMemberStockList(params);
			return R.ok().put("data", mapList);

		}
		
	}

	@Override
	public R getMemberStockRecordList(Map<String, Object> params) {
		//获取所有的年跟月
		List<String> dateList = memberStockDao.getMemberStockDate(params);
		if(dateList.size()>0){
			
			List<Map<String, Object>> returnList = new ArrayList<>();
			for (String string : dateList) {
				params.put("stockDate", string);
				List<Map<String, Object>> recordList = memberStockDao.getMemberStockRecordList(params);
				Map<String, Object> map = new HashMap<>();
				map.put("stockDate", string);
				map.put("recordList", recordList);
				returnList.add(map);
			}
			return R.ok().put("data", returnList);
		}
		return R.ok().put("data", new ArrayList<>());
	}
	
}
